package com.delu.shiro;

import com.delu.entity.User;
import com.delu.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class LoginRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权方法");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证方法");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String usernamer = token.getUsername();
        String password = String.valueOf(token.getPassword());
        System.out.println(usernamer);
        User user = userService.selectByName(usernamer);
        System.out.println(user);
        if (user == null) {
            return null;//用户不存在
        }
        System.out.println(token.getPassword());
        return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), user.getUsrname());

    }
}
